package com.ruix.app.util;



import android.content.Context;
        import android.os.CountDownTimer;
        import android.widget.TextView;

public class CountDownTimerUtil extends CountDownTimer {


    private static final long DEFAULT_MILLIS_IN_FUTURE = 60000;// 默认－60秒
    private static final long DEFAULT_COUNT_DOWN_INTERVAL = 1000;// 默认－1秒

    private TextView textView;// 倒计时所依附的view
    private int strResId;// 秒＋文字［通过占位符实现］
    private ICountDownTimerInterface iCdtInterface;
    private ICountDownTimerUpdate timerUpdate;
    private String strRes;

    private Context context;

    public CountDownTimerUtil(Context context, ICountDownTimerInterface iCdtInterface, TextView textView, int strResId) {// 默认60秒
        // 第一个参数是总的时间,第二个参数是间隔时间（毫秒）
        super(DEFAULT_MILLIS_IN_FUTURE, DEFAULT_COUNT_DOWN_INTERVAL);
        this.context = context;
        this.iCdtInterface = iCdtInterface;
        this.textView = textView;
        this.strResId = strResId;
        buildStrRes();
    }


    public CountDownTimerUtil(Context context, ICountDownTimerInterface iCdtInterface, TextView textView, int strResId, long millisInFuture) {// 自定义计时时长／毫秒
        // 第一个参数是总的时间,第二个参数是间隔时间（毫秒）
        super(millisInFuture, DEFAULT_COUNT_DOWN_INTERVAL);
        this.context = context;
        this.iCdtInterface = iCdtInterface;
        this.textView = textView;
        this.strResId = strResId;
        buildStrRes();
    }

    public CountDownTimerUtil(Context context, ICountDownTimerInterface iCdtInterface, TextView textView, int strResId, long millisInFuture, long countDownInterval) {// 自定义计时时长／毫秒+间隔时长
        // 第一个参数是总的时间,第二个参数是间隔时间（毫秒）
        super(millisInFuture, countDownInterval);
        this.context = context;
        this.iCdtInterface = iCdtInterface;
        this.textView = textView;
        this.strResId = strResId;
        buildStrRes();
    }

    public CountDownTimerUtil(ICountDownTimerInterface iCdtInterface, ICountDownTimerUpdate timerUpdate, String strRes, long millisInFuture, long countDownInterval) {// 自定义计时时长／毫秒+间隔时长
        // 第一个参数是总的时间,第二个参数是间隔时间（毫秒）
        super(millisInFuture, countDownInterval);
        this.strRes = strRes;
        this.iCdtInterface = iCdtInterface;
        this.timerUpdate = timerUpdate;
    }

    private void buildStrRes(){
        if(strResId > 0 && context != null){
            strRes = context.getString(strResId);
        }
    }

    @Override
    public void onTick(long millisUntilFinished) {// 打点
        String updateText;
        if(strRes != null){
            updateText = String.format(strRes, millisUntilFinished / 1000);
        }else {
            updateText = String.valueOf(millisUntilFinished / 1000);
        }
        if(timerUpdate != null){
            timerUpdate.setText(updateText);
        }else if(textView != null){
            textView.setText(updateText);
        }
    }

    @Override
    public void onFinish() {// 结束
        destroy();
        if (iCdtInterface != null) {
            iCdtInterface.countDownTimerFinish();
        }
    }

    private void destroy() {// 自我销毁
        super.cancel();
        textView = null;// 资源断开引用，等待GC
        timerUpdate = null;
    }

    public interface ICountDownTimerInterface {
        void countDownTimerFinish();// 通知调用者倒计时结束，进行后续操作
    }

    public interface ICountDownTimerUpdate {
        void setText(String text);  // 更新倒计时
    }
}